﻿using System;
using System.Collections.Generic;
using NthDownload.BL;
using System.ComponentModel.Composition;
using NthDownload.Contracts;
using NthDownload.Common.ViewModels;
using NthDownload.Common.Mapping;
using NthDownload.Common.Utils;

namespace NthDownload.ViewModels
{
    public class QueuesViewModel : ViewModelConductorBase
    {
        #region Class Members

        private NthProfile _profile;
        private Dictionary<NthQueue, QueueViewModel> _queueViewModels;

        #endregion Class Members

        #region Constructors

        public QueuesViewModel()
        {
            //
            // Set defaults
            //
            QueueViewModels = new Dictionary<NthQueue, QueueViewModel>();

            //
            // If there are queues show the first one
            //
            if (Profile.Queues.Count > 0)
            {
                ShowQueue(Profile.Queues[0]);
            }
        }

        #endregion Constructors

        #region Properties

        /// <summary>
        /// The profile
        /// </summary>
        [Import(AllowRecomposition = true)]
        public NthProfile Profile
        {
            get
            {
                return _profile;
            }
            set
            {
                _profile = value;
                NotifyOfPropertyChange(() => Profile);
            }
        }

        #endregion Properties

        #region Private Properties

        /// <summary>
        /// The exiting queue view models
        /// </summary>
        private Dictionary<NthQueue, QueueViewModel> QueueViewModels
        {
            get
            {
                return _queueViewModels;
            }
            set
            {
                _queueViewModels = value;
                NotifyOfPropertyChange(() => QueueViewModels);
            }
        }

        #endregion Private Properties

        #region Methods

        /// <summary>
        /// Adds a new queue to the profile
        /// </summary>
        public void AddNewQueue()
        {
            NthQueue newQueue = new NthQueue();
            newQueue.Name = "New Queue";

            QueueDetailsViewModel queueDetailsViewModel = new QueueDetailsViewModel(newQueue, Profile);

            if (NavigationService.ShowDialog(queueDetailsViewModel, false) == ViewModelResults.Save)
            {
                Profile.AddQueue(newQueue);
                ShowQueue(newQueue);
            }
        }

        /// <summary>
        /// Shows the details screen for the given queue
        /// </summary>
        public void EditQueue(NthQueue queue)
        {
            NthQueue clonedQueue = (NthQueue)ObjectMappingService.Clone(queue);

            QueueDetailsViewModel queueDetailsViewModel = new QueueDetailsViewModel(clonedQueue, Profile);

            if (NavigationService.ShowDialog(queueDetailsViewModel, false) == ViewModelResults.Save)
            {
                ObjectMappingService.Update(clonedQueue, queue);
            }
        }

        /// <summary>
        /// Adds a new queue to the profile
        /// </summary>
        public void RemoveQueue(NthQueue queue)
        {
            MessageDialogViewModel messageDialogViewModel = new MessageDialogViewModel("Are you sure you'd like to remove '" + queue.Name + "'?", ViewModelResults.Yes)
            {
                ShowYes = true,
                ShowNo = true
            };

            if (NavigationService.ShowDialog(messageDialogViewModel, false) != ViewModelResults.Yes)
            {
                return;
            }

            try
            {
                Profile.RemoveQueue(queue);
            }
            catch (Exception e)
            {
                MessageDialogViewModel.ShowError(ExceptionUtils.GetInnerMostException(e).Message);
            }
        }

        /// <summary>
        /// Shows a queue
        /// </summary>
        /// <param name="queue">The queue to show.</param>
        public void ShowQueue(NthQueue queue)
        {
            //
            // Deselect all queues
            //
            foreach (NthQueue q in Profile.Queues)
            {
                q.IsSelected = false;
            }

            //
            // Select queue that is going to be shown
            //
            queue.IsSelected = true;

            //
            // Try get queue view model from cache
            //
            QueueViewModel queueViewModel;
            if (!QueueViewModels.TryGetValue(queue, out queueViewModel))
            {
                //
                // If no view was found in the cache create new view and add to cache
                //
                queueViewModel = new QueueViewModel(queue);
                QueueViewModels.Add(queue, queueViewModel);
            }

            ActivateItem(queueViewModel);
        }

        #endregion Methods
    }
}
